package com.ahao.userprivacy;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class AhaoPrivacyDialog extends Dialog {

    private onAgreeListener mOnAgreeListener;
    private ViewGroup layout;

    public interface onAgreeListener {
        void onAgree();

        void onDisagree();
    }

    public AhaoPrivacyDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 移除标题栏
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        // 移除背景
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        super.onCreate(savedInstanceState);
        layout = (ViewGroup) getLayoutInflater().inflate(getContext().getResources().getIdentifier("ahao_privacy_dialog", "layout", getContext().getPackageName()), null);
        setContentView(layout);
        initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(layoutParams);
        // 设置布局以外的
    }

    private void initView() {
        // 从application的meta-data中获取公司名称
        try {
            String companyName = "";
            Bundle metData = getContext().getApplicationContext().getPackageManager().getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA).metaData;
            companyName = metData.getString("ahao_company_name");
            String userPrivacyUrl = metData.getString("ahao_user_privacy_url");
            String userPrivacy2Url = metData.getString("ahao_user_privacy2_url");

            findViewById(getContext().getResources().getIdentifier("ahao_privacy_dialog_agree", "id", getContext().getPackageName())).setOnClickListener(v -> {
                if (mOnAgreeListener != null) {
                    mOnAgreeListener.onAgree();
                }
            });
            findViewById(getContext().getResources().getIdentifier("ahao_privacy_dialog_disagree", "id", getContext().getPackageName())).setOnClickListener(v -> {
                if (mOnAgreeListener != null) {
                    mOnAgreeListener.onDisagree();
                }
            });
            TextView companyNameTv = findViewById(getContext().getResources().getIdentifier("ahao_id_tv_policy_text", "id", getContext().getPackageName()));
            String oldText = companyNameTv.getText().toString();
            companyNameTv.setText(oldText.replace("[ahao_company]", companyName));
            // companyNameTv文本内容设置两端对齐,通过html文本加入css属性
            String html = "<html></head><body style=\"text-align:justify;\">" + companyNameTv.getText().toString().replaceAll(" ", "&nbsp;").replaceAll("\n","<br/>") + "</body></html>";
            companyNameTv.setText(android.text.Html.fromHtml(html));


            setCancelable(false);
            findViewById(getContext().getResources().getIdentifier("ahao_id_agreement_policy_tv1", "id", getContext().getPackageName())).setOnClickListener(v -> {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.setData(android.net.Uri.parse(userPrivacyUrl));
                getContext().startActivity(intent);
            });
            findViewById(getContext().getResources().getIdentifier("ahao_id_agreement_policy_tv3", "id", getContext().getPackageName())).setOnClickListener(v -> {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.setData(android.net.Uri.parse(userPrivacy2Url));
                getContext().startActivity(intent);
            });
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public AhaoPrivacyDialog setOnAgreeListener(onAgreeListener mOnAgreeListener) {
        this.mOnAgreeListener = mOnAgreeListener;
        return this;
    }
}
